1937B - Binary Path - CodeForces Solution


implementation

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int  long long
#define test int t;cin>>t;while(t--)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl '\n'
using namespace std;

const int M=1e9+7;

int nCr(int n, int r) {
    int sum=1;
     for(int i = 1; i <= r; i++){
            sum = sum * (n - r + i) / i;
        }
    return sum;
}
//O(log(min(a,b)))
int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a % b);
}   
//O(log(min(a,b)))
int lcm(int a, int b){
    return (a / gcd(a, b)) * b;
}

//O(n)
int maxSubArraySum(int a[], int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0;
 
    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
 
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

int sqroot(int n){
    int l = 0, h = 1000000000,ans = n;
    while(l <= h){
        int mid = l + (h-l)/2;
        if( mid*mid <= n){
            ans = mid;
            l = mid + 1;
        }
        else
            h = mid - 1;
    }
    return ans;
}


bool isPowerOfTwo(unsigned int n){
return (n&&!(n&n-1));
}

int countSetBits(int n){
    int count=0;
    while(n){
        n=n&(n-1);
        count++;
    }
    return count;
}

void prime(int n){
    for(int i=0;i*i<n;i++){
        while(n%i == 0)
        {
            cout<<i<<" ";
            n=n/i;
        }
    }
    if(n>1) cout<<n<<" ";
}

bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i <= n / 2; i++)
        if (n % i == 0)
            return false;
  
    return true;
}

int power(int a,int b){
    if(b==0) return 1;
    int tmp=power(a,b/2);
    int result=((tmp%M)*(tmp%M))%M;
    if(b%2==1) result =((result%M)*(a%M))%M;
    return result;
}

//int div(int a,int b){
//return a*power(b,M-2)%M;
//}

int mul(int a,int b){
return ((a%M)*(b%M)%M);
}


//___________________________________________________________________//

//FOR DEBUGGING
#define printarr(arr) for(int i=0;i<(sizeof(arr)/sizeof(arr[0]));i++){ cout << arr[i] << " ";}cout << endl;
#define printvec(v) for(int i=0;i<v.size();i++){ cout << v[i] << " ";}cout << endl;
#define printset(s) for(auto i : s){cout << i << " ";}cout<<endl;

//____________________________________________________________________//


void solve(){
    test{
        int n;
        cin>>n;
        string s1,s2;
        cin>>s1>>s2;
        string ans;
        int last=n-1;
        int cnt=0;
        for(int i=0;i<n-1;i++){
            if(s1[i+1]>s2[i]){
                last=i;
                break;
            }
        }
        //cout<<last<<endl;
        for(int i=last-1;i>=0;i--){
            if(s1[i+1]==s2[i]){
                cnt++;
            }
            else
            break;
        }
        for(int i=0;i<=last;i++){
            cout<<s1[i];
        }
        for(int i=last;i<n;i++){
            cout<<s2[i];
        }
        cout<<endl;
        cout<<cnt+1<<endl;

    }
}

signed main(){

    //do-not-use this in case of interactive
    ios_base::sync_with_stdio(0); //
    cin.tie(0);                   //
    //   //   //  //  //  //  //  //  

    solve();

    //IF YOU ARE GETTING ERROR:

    //RUNTIME: 1.Check if all indices are within bounds
    //         2.Check if div by zero is not happening

    //TLE:     1.Check if infinite loop is formed at any test-case
    //         2.Calculate time complexity(10e8 oper === 1sec) 

    //WA:      1.See if you are missing any edge-case(like n=1)
    //         2.Re-read the question
   
}


Comments

Submit
1 Comments
  • 1/3/2024 13:31 - Asia/Calcutta

hi


More Questions

1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III